package com.zfei.gfb.api.domain;

import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.function.LongSupplier;

/**
 * @author fgh
 */
public class Page implements Serializable {

    private Long minTime;

    private Long maxTime;

    private Integer offset;



    @ApiModelProperty("分页条数，默认20")
    private Integer limit;

    private Integer orderType;

    private String orderCol;

    @ApiModelProperty("第几页")
    private Integer pageNo;

    private Integer count;

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
        this.limit = count;
    }

    /**
     * 优质优先
     */
    private boolean highQuality = false;

    public Page() {
    }

    public Page(Long minTime, Long maxTime, Integer offset, Integer limit, Integer orderType) {
        this(minTime, maxTime, offset, limit, orderType, null);
    }

    public Page(Long minTime, Long maxTime, Integer offset, Integer limit, Integer orderType, String orderCol) {
        this.minTime = minTime;
        this.maxTime = maxTime;
        this.offset = offset;
        this.limit = limit;
        this.count = limit;
        this.orderType = orderType;
        this.orderCol = orderCol;
    }



    public Long getMinTime() {
        return minTime;
    }

    public void setMinTime(Long minTime) {
        this.minTime = minTime;
    }

    public Long getMaxTime() {
        return maxTime;
    }

    public void setMaxTime(Long maxTime) {
        this.maxTime = maxTime;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLimit() {
        return limit;
    }

    public Page setLimit(Integer limit) {
        this.limit = limit;
        return this;
    }

    public Integer getOrderType() {
        return orderType;
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
    }

    public String getOrderCol() {
        return orderCol;
    }

    public void setOrderCol(String orderCol) {
        this.orderCol = orderCol;
    }

    public Page build(LongSupplier supplier) {
        if (minTime == null || minTime < 0) {
            minTime = 0L;
        }
        if (maxTime == null || maxTime <= 0) {
            maxTime = supplier.getAsLong();
        }
        if (limit == null || limit <= 0) {
            limit = 20;
        }

        if (count != null) {
            limit = count;
        }

        if (offset == null || offset < 0) {
            offset = 0;
        }
        if (pageNo != null && pageNo > 0) {
            offset = (pageNo - 1) * limit;
        }
        if (orderType == null || orderType != OrderType.ASC) {
            orderType = OrderType.DESC;
        }
        //query参数如传""，转化为null
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object val = field.get(this);
                if (val instanceof String && StringUtils.isBlank((String) val)) {
                    field.set(this, null);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return this;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public boolean isHighQuality() {
        return highQuality;
    }

    public void setHighQuality(boolean highQuality) {
        this.highQuality = highQuality;
    }
}
